import * as vscode from "vscode";

let timerId: NodeJS.Timeout | null = null;
let isReminderEnabled = true;

function startReminder() {
  if (timerId !== null) {
    clearTimeout(timerId);
  }
  if (isReminderEnabled) {
    timerId = setTimeout(() => {
      vscode.window.showInformationMessage("Time to take a break!");
      startReminder();
    }, 25 * 60 * 1000);
  }
}

function toggleReminder() {
  isReminderEnabled = !isReminderEnabled;

  vscode.window.showInformationMessage(
    `Reminder is now ${isReminderEnabled ? "enabled" : "disabled"}.`
  );

  if (isReminderEnabled) {
    startReminder();
  } else {
    if (timerId !== null) {
      clearTimeout(timerId);

      timerId = null;
    }
  }
}

export function activate(context: vscode.ExtensionContext) {
  console.log('Congratulations, your extension "rest" is now active!');
  let disposable = vscode.commands.registerCommand("rest.hi", () => {
    vscode.window.showInformationMessage("Hello World from rest!");
  });

  startReminder();
  context.subscriptions.push(
    vscode.commands.registerCommand("rest.click", toggleReminder)
  );
  context.subscriptions.push(disposable);
}

export function deactivate() {
  if (timerId !== null) {
    clearTimeout(timerId);
    timerId = null;
  }
}
